'''
Created on 25/03/2009

@author: eh01
'''
from im.ds.dst import DST
from im.ds import dsconstants
from im.ds.dso import DSO

def create_dso_for_im_http_response_handler(response_code, content_type, body, request, lock):
    '''
    creates the dso for the IMHTTPResponseHandler
    '''
    dst1 = DST(subject = dsconstants.SUBJECT_ROOT, 
              predicate = dsconstants.PREDICATE_HTTP_RESPONSE_CODE, 
              literal = response_code)
    dst2 = DST(subject = dsconstants.SUBJECT_ROOT, 
              predicate = dsconstants.PREDICATE_HTTP_RESPONSE_CONTENT_TYPE, 
              literal = content_type)
    dst3 = DST(subject = dsconstants.SUBJECT_ROOT, 
              predicate = dsconstants.PREDICATE_HTTP_RESPONSE_BODY,
              literal = body)
    dst4 = DST(subject = dsconstants.SUBJECT_ROOT, 
              predicate = dsconstants.PREDICATE_HTTP_REQUEST,
              literal = request)
    dst5 = DST(subject = dsconstants.SUBJECT_ROOT, 
              predicate = dsconstants.PREDICATE_LOCK,
              literal = lock)    
    dsts = [dst1, dst2, dst3, dst4, dst5]
    dso = DSO(dsts)
    return dso

def create_dso_for_http_error_handler(error_code, error_message, request, lock):
    '''
    creates the dso for the HTTPErrorHandler
    '''
    dst1 = DST(subject = dsconstants.SUBJECT_ROOT, 
              predicate =  dsconstants.PREDICATE_HTTP_ERROR, 
              object = dsconstants.SUBJECT_HTTP_ERROR)
    dst2 = DST(subject = dsconstants.SUBJECT_HTTP_ERROR,
               predicate = dsconstants.PREDICATE_ERROR_CODE,
               literal = error_code)
    dst3 = DST(subject = dsconstants.SUBJECT_ROOT, 
              predicate = dsconstants.PREDICATE_HTTP_REQUEST,
              literal = request)
    dst4 = DST(subject = dsconstants.SUBJECT_ROOT, 
              predicate = dsconstants.PREDICATE_LOCK,
              literal = lock)
    dst5 = None
    if error_message:
        dst5 = DST(subject = dsconstants.SUBJECT_HTTP_ERROR,
                   predicate = dsconstants.PREDICATE_ERROR_MESSAGE,
                   literal = error_message)
        
    if dst5:
        dsts = [dst1, dst2, dst3, dst4, dst5]
    else:
        dsts = [dst1, dst2, dst3, dst4]
        
    dso = DSO(dsts)
    return dso   